/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.manage.plan;

import shop.core.common.bean.JSONResult;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.plan.PlanService;
import shop.services.manage.plan.bean.Plan;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.product.ProductService;
import shop.services.manage.product.bean.Product;

import java.util.ArrayList;
import java.util.List;

/**
 * @类名称：PlanAction
 * @创建人：wzl
 * @创建时间：2018-05-09 下午19:23:04
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/manage/plan/")
public class PlanAction extends BaseController<Plan> {
    private static final Logger logger = LoggerFactory.getLogger(PlanAction.class);
    @Autowired
    private PlanService planService;
    @Autowired
    private ProductService productService;
    private static final String page_toList = "/manage/scheme/planList";
    private static final String page_toAdd = "/manage/scheme/planAdd";
    private static final String page_toEdit = "/manage/scheme/planEdit";

    public PlanService getService() {
        return planService;
    }

    private PlanAction() {
        super.page_toList = page_toList;
        super.page_toAdd = page_toAdd;
        super.page_toEdit = page_toEdit;
    }

    /**
     * 查询方案的行业需求列表
     *
     * @param plan
     * @return
     */
    @RequestMapping(value = "selectProfessionList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProfessionList(Plan plan) {
        plan = getService().selectOne(plan);
        String[] professionImage = plan.getProfessionImage().split(",");
        String[] professionName = plan.getProfessionName().split(";");
        String[] firstProfessionContent = plan.getFirstProfessionContent().split(";");
        String[] secondProfessionContent = plan.getSecondProfessionContent().split(";");
        String[] thirdProfessionContent = plan.getThirdProfessionContent().split(";");
        List<Plan> planList = new ArrayList<>();
        for (int i = 0; i < professionImage.length; i++) {
            Plan planInfo = new Plan();
            planInfo.setProfessionImage(professionImage[i]);
            planInfo.setProfessionName(professionName[i]);
            planInfo.setFirstProfessionContent(firstProfessionContent[i]);
            planInfo.setSecondProfessionContent(secondProfessionContent[i]);
            planInfo.setThirdProfessionContent(thirdProfessionContent[i]);
            planList.add(planInfo);
        }
        jsonResult = new JSONResult();
        jsonResult.setData(planList);
        return jsonResult;
    }

    /**
     * 查询方案的产品推荐列表
     *
     * @param plan
     * @return
     */
    @RequestMapping(value = "selectProductList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProductList(Plan plan) {
        plan = getService().selectOne(plan);
        String[] productID = plan.getProductID().split(",");
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < productID.length; i++) {
            Product product = new Product();
            product.setId(productID[i]);
            product = productService.selectOne(product);
            if (product != null)
                productList.add(product);
        }
        jsonResult = new JSONResult();
        jsonResult.setData(productList);
        return jsonResult;
    }
}
